#include "qmatchedpathmodel.h"


QMatchedPathModel::QMatchedPathModel(vector<MatchedPath> *matches, QObject* parent) : QAbstractTableModel(parent)
{
    matched_paths = matches;    
}

int QMatchedPathModel::rowCount(const QModelIndex &index) const
{
    return matched_paths->size();

}

int QMatchedPathModel::columnCount(const QModelIndex &index) const
{
    return 5;
}


// returns the item of data that corresponds to the index argument
QVariant QMatchedPathModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    if (index.row() < 0 || index.row() >= matched_paths->size())
        return QVariant();


    if(role==Qt::CheckStateRole)
    {
        if(index.column()==0)
            return Qt::Checked;
    }


    if (role == Qt::DisplayRole)
    {
        MatchedPath match = matched_paths->at(index.row());

        switch(index.column())
        {


             case 1:
            return (match.rule.rule_name).c_str();

             case 2:
            return (Vars::get_action(match.rule.action)).c_str();

             case 3:
            return (match.path.string()).c_str();

             case 4:
            return (match.to_path.string()).c_str();

             default:
            return QVariant();

        }
    }

    return QVariant();
}

QVariant QMatchedPathModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if(role!=Qt::DisplayRole)
        return QVariant();

    if (role != Qt::DisplayRole)
        return QVariant();



    if (orientation == Qt::Horizontal) {
        switch (section) {
             case 0:
            return tr("Rule Id");

             case 1:
            return tr("Rule Name");

             case 2:
            return tr("Action");

             case 3:
            return tr("Matched Path");

             case 4:
            return tr("New Path");

             default:
            return QVariant();
        }
    }
    return QVariant();
}





bool QMatchedPathModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (index.isValid() && role == Qt::EditRole)
    {
        int row = index.row();
        emit(dataChanged(index, index));

        return true;
    }

    return false;
}


// need to implement this for editable models
Qt::ItemFlags QMatchedPathModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return Qt::ItemIsEnabled;

    return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}

bool QMatchedPathModel::insertRows(int position, int rows, const QModelIndex &index)
{
    return true;

}
bool QMatchedPathModel::removeRows(int position, int rows, const QModelIndex &index)
{
    return true;
}


